package com.hq.modeapp.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hq.modeapp.R;
import com.hq.modeapp.bean.Photo_Bean;
import com.hq.modeapp.bean.Photo_List_Bean;
import com.hq.modeapp.bean.Video_Bean;
import com.hq.modeapp.manager.AppContext;
import com.hq.modeapp.ui.PhotoViewFlowActivity;
import com.hq.modeapp.ui.VideoPlayerActivity;

public class VideoListViewAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<Video_Bean> list = new ArrayList<Video_Bean>();
	private Context context;
	private AppContext appContext;

	public VideoListViewAdapter(Context context,
			ArrayList<Video_Bean> list) {

		this.mInflater = LayoutInflater.from(context);
		this.context = context;
		this.appContext = (AppContext)(context.getApplicationContext());
		this.setList(list);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list.size()==0){
			return 0;
		}
		return getList().size() / 2;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return getList().get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		final Video_Bean bean_Photo_List1 = getList().get(position * 2);
		final Video_Bean bean_Photo_List2 = getList()
				.get(position * 2 + 1);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.photo_set_item, null);
			View view1 = convertView.findViewById(R.id.picture_item1);
			holder.setImg_item1((ImageView) view1.findViewById(R.id.img_item));
			holder.setTv_item1((TextView) view1.findViewById(R.id.tv_item));
			View view2 = convertView.findViewById(R.id.picture_item2);
			holder.setImg_item2((ImageView) view2.findViewById(R.id.img_item));
			holder.setTv_item2((TextView) view2.findViewById(R.id.tv_item));
			holder.getImg_item1().setOnClickListener(new MyOnClickListener());
			holder.getImg_item2().setOnClickListener(new MyOnClickListener());
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.getImg_item1().setTag(bean_Photo_List1);
		holder.getImg_item2().setTag(bean_Photo_List2);

		holder.getTv_item1().setText(bean_Photo_List1.getTitile());
		holder.getTv_item2().setText(bean_Photo_List2.getTitile());
		final ViewHolder holder2 = holder;

		holder.getImg_item1().setImageDrawable(
				context.getResources().getDrawable(R.drawable.default_news));
		holder.getImg_item2().setImageDrawable(
				context.getResources().getDrawable(R.drawable.default_news));

		appContext.finalBitmap.display(holder2.getImg_item1(), bean_Photo_List1.getLogo());
		appContext.finalBitmap.display(holder2.getImg_item2(), bean_Photo_List2.getLogo());
		System.out.println("图片1url="+bean_Photo_List1.getLogo());
		System.out.println("图片2url="+bean_Photo_List2.getLogo());
		return convertView;
	}

	/**
	 * @BareFieldName : list
	 * 
	 * @return the list
	 * 
	 * 
	 **/

	public ArrayList<Video_Bean> getList() {
		return list;
	}

	/**
	 * @BareFieldName : list
	 * 
	 * @return the list
	 * 
	 * @param list
	 *            the list to set
	 * 
	 **/

	public void setList(ArrayList<Video_Bean> list) {
		this.list.clear();
		this.list.addAll(list);
	}

	// �Զ���������
	class ViewHolder {
		private TextView tv_item1;
		private ImageView img_item1;
		private TextView tv_item2;
		private ImageView img_item2;

		/**
		 * @BareFieldName : tv_item1
		 * 
		 * @return the tv_item1
		 * 
		 * 
		 **/

		public TextView getTv_item1() {
			return tv_item1;
		}

		/**
		 * @BareFieldName : tv_item1
		 * 
		 * @return the tv_item1
		 * 
		 * @param tv_item1
		 *            the tv_item1 to set
		 * 
		 **/

		public void setTv_item1(TextView tv_item1) {
			this.tv_item1 = tv_item1;
		}

		/**
		 * @BareFieldName : img_item1
		 * 
		 * @return the img_item1
		 * 
		 * 
		 **/

		public ImageView getImg_item1() {
			return img_item1;
		}

		/**
		 * @BareFieldName : img_item1
		 * 
		 * @return the img_item1
		 * 
		 * @param img_item1
		 *            the img_item1 to set
		 * 
		 **/

		public void setImg_item1(ImageView img_item1) {
			this.img_item1 = img_item1;
		}

		/**
		 * @BareFieldName : img_item2
		 * 
		 * @return the img_item2
		 * 
		 * 
		 **/

		public ImageView getImg_item2() {
			return img_item2;
		}

		/**
		 * @BareFieldName : img_item2
		 * 
		 * @return the img_item2
		 * 
		 * @param img_item2
		 *            the img_item2 to set
		 * 
		 **/

		public void setImg_item2(ImageView img_item2) {
			this.img_item2 = img_item2;
		}

		/**
		 * @BareFieldName : tv_item2
		 * 
		 * @return the tv_item2
		 * 
		 * 
		 **/

		public TextView getTv_item2() {
			return tv_item2;
		}

		/**
		 * @BareFieldName : tv_item2
		 * 
		 * @return the tv_item2
		 * 
		 * @param tv_item2
		 *            the tv_item2 to set
		 * 
		 **/

		public void setTv_item2(TextView tv_item2) {
			this.tv_item2 = tv_item2;
		}

	}

	/**
	 * **********************************************************
	 * 
	 * @����ժҪ �������ղء�ɾ��ť�ļ���
	 * 
	 * @���� ��Administrator
	 * @����ʱ�� ��2012-11-1 ����2:46:45
	 * @��ʷ��¼ :
	 * @���� : 2012-11-1 ����2:46:45 �޸��ˣ�����
	 * @���� :
	 *********************************************************** 
	 */
	class MyOnClickListener implements OnClickListener {
		private ArrayList<Photo_Bean> list;
		private Photo_Bean lastPhoto;

		public MyOnClickListener() {
			super();
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.img_item:

				Video_Bean bean = (Video_Bean) v.getTag();
				ImageView img_item = (ImageView) v;
//				System.out.println("urlist.url="+urlList.get(0));
				Intent intent = new Intent(context, VideoPlayerActivity.class);
//				intent.putExtra("path","/mnt/sdcard/DCIM/100ANDRO/MOV_0141.mp4");
				intent.putExtra("path",bean.getVideo_url());
				intent.putExtra("title","MOV_0141");
				context.startActivity(intent);
				System.out.println("bean.url==="+bean.getVideo_url());
			default:
				break;
			}
		}
	}
}
